package com.base.study04_collection;

import java.util.ArrayList;
import java.util.function.Consumer;

/**
 * ClassName: ForEachStudy
 * PackageName: com.cwy.study04_collection
 * Describe:
 *
 * @author: Cheng Wenying
 * @Create: 2024/1/30
 * @version:
 */
public class ForEachStudy {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("nihao");
        list.add("hahah");
        list.add("xiaoming");
        list.forEach(System.out::println);
        // 原始写法
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        // 不要在foreach中对list集合进行add/remove操作，否则会报出异常
        for (String s : list) {
            if("nihao".equals(s)){
                list.remove(s);
            }
        }
    }
}
